package com.cm386.final_project_timezonealert.model;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TimePicker;

import com.cm386.final_project_timezonealert.EventFieldActivity;
import com.cm386.final_project_timezonealert.MainActivity;

public class TimePickerDialogFragment extends DialogFragment{
	
	private static TimePickerDialogFragment timePicerDialogFragment;
	public static final String INIT_HOUR_KEY 	   = "INIT_HOUR";
	public static final String INIT_MINUTES_KEY    = "INIT_MINUTES";
	public static final String POSITION_KEY		   = "POSITION";
	public static final String EVENT_CITY_NAME_KEY	   = "EVENT_CITY_NAME";
	public static final String EVENT_CITY_TIMEZONE_ID_KEY = "EVENT_CITY_TIMEZONE_ID";
	
	
	
	public static TimePickerDialogFragment getInstance(int initHour, int initMinutes, int position, City eventCity){
		if (timePicerDialogFragment == null)
			timePicerDialogFragment = new TimePickerDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(INIT_HOUR_KEY, initHour);
		bundle.putInt(INIT_MINUTES_KEY, initMinutes);
		bundle.putInt(POSITION_KEY, position);
		bundle.putString(EVENT_CITY_NAME_KEY, eventCity.getName());
		bundle.putString(EVENT_CITY_TIMEZONE_ID_KEY, eventCity.getTimeZoneId());
		timePicerDialogFragment.setArguments(bundle);
		return timePicerDialogFragment;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		int initHour 	= getArguments().getInt(INIT_HOUR_KEY);
		int initMinutes = getArguments().getInt(INIT_MINUTES_KEY);
		int position	= getArguments().getInt(POSITION_KEY);
		String eventCityName = getArguments().getString(EVENT_CITY_NAME_KEY);
		String eventCityTimeZoneID = getArguments().getString(EVENT_CITY_TIMEZONE_ID_KEY);

		// Use the Builder class for convenient dialog construction
		MyTimePickerDialog dialog = new MyTimePickerDialog(getActivity(),new OnTimeSetListener() {  
			int initHour;
			int initMinutes;
			int position;
			
			private OnTimeSetListener init(int initHour, int initMinutes, int position) {
				// TODO Auto-generated method stub
				this.initHour 	 = initHour;
				this.initMinutes = initMinutes;
				this.position	 = position;
				return this;
			}
			
			
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
				// TODO Auto-generated method stub  
				if (hourOfDay != initHour 	||
					minute	  != initMinutes){
					MainActivity callingActivity = (MainActivity) getActivity();
					callingActivity.timePickerSetTimeCallBack(hourOfDay, minute, position);
				}
				//System.out.println("hour-->"+hourOfDay+"  minute-->"+ minute);  
			}
			
		}.init(initHour, initMinutes, position), initHour, initMinutes, true, eventCityName, eventCityTimeZoneID);
		
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Event", 
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int updateHour   = ((MyTimePickerDialog)dialog).getUpdateHour();
                int updateMinute = ((MyTimePickerDialog)dialog).getUpdateMinute();
                Context context  = ((MyTimePickerDialog)dialog).getContext();
                String eventCityName = ((MyTimePickerDialog)dialog).getEventCityName();
                String eventCityTimeZoneId = ((MyTimePickerDialog)dialog).getEventCityTimeZoneId();
                System.out.println("Hour : " + updateHour + "  " + "Minute: " + updateMinute);
                Intent eventFieldActivity = new Intent(context, EventFieldActivity.class);
                Bundle data = new Bundle();
                data.putString(EVENT_CITY_NAME_KEY, eventCityName);
                data.putString(EVENT_CITY_TIMEZONE_ID_KEY, eventCityTimeZoneId);
                data.putInt("HOUR", updateHour);
                data.putInt("MINUTE", updateMinute);
                eventFieldActivity.putExtras(data);
				startActivity(eventFieldActivity);
				dialog.dismiss();
            }
        });
        
		
		// TODO Auto-generated method stub
		return dialog;
	}
}
